package com.ct;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.ct.config.mybatisPlus.RequestDataHelper;
import com.ct.model.entity.kk_ycz.Order;
import com.ct.model.entity.kkrot.User;
import com.ct.server.kk_ycz.IOrderService;
import com.ct.server.kkrot.IUserService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@SpringBootTest
public class MysqlServerDemo {
    @Resource
    public IUserService iUserService;

    @Resource
    public IOrderService iOrderService;

    //查询
    @Test
    public void slectTest() {
        List<User> list = iUserService.list();
        StaticLog.info("{}", list);
    }

    //动态表名查询
    @Test
    public void bTest() {
        try {
            for (int i = 0; i < 4; i++) {
                RequestDataHelper.setRequestData(MapUtil.builder(new HashMap<String, Object>()).put("user", i).build());
                List<User> userList = iUserService.list();
                StaticLog.info("userName:{}", JSONUtil.toJsonStr(userList));
            }
        } finally {
            RequestDataHelper.removeRequestData();
        }
    }

    //插入
    @Test
    public void insertTest() {
        Order order = new Order();
        order.setUsername("k1");
        order.setOrderNumber("1");
        iOrderService.save(order);
    }

    //批量插入
    @Test
    public void dTest() {
        final ArrayList<Order> orderArrayList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Order order = new Order();
            order.setUsername(StrUtil.format("k{}", i));
            order.setOrderNumber(Convert.toStr(i));
            orderArrayList.add(order);
        }

        iOrderService.saveBatch(orderArrayList);
    }

    @Test
    public void eTest() {
    }
}
